package com.software.practicecenter.service;

import com.software.practicecenter.controller.QuestionController;
import com.software.practicecenter.entity.PracticeRecord;
import com.software.practicecenter.entity.Question;
import com.software.practicecenter.entity.SubPassage;
import com.software.practicecenter.entity.SubPassageDTO;
import com.software.practicecenter.repository.PracticeRecordRepository;
import com.software.practicecenter.repository.SubPassageRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

@Service
public class SubPassageService {
    @Autowired
    private SubPassageRepository subPassageRepository;

    @Autowired
    private PracticeRecordRepository practiceRecordRepository;

    @Autowired
    private QuestionController questionController;

    public List<SubPassageDTO> findByPassageId(String passageId) {

        List<SubPassage> subPassages = subPassageRepository.findByPassageId(passageId);
        List<SubPassageDTO> subPassageDTOS = new ArrayList<>();
        for (int i=0;i<subPassages.size();i++){
            SubPassageDTO subPassageDTO = new SubPassageDTO();
            String no = subPassages.get(i).getNo();
            String name = subPassages.get(i).getName();
            Double averageAccuracy = subPassages.get(i).getAverageAccuracy();
            subPassageDTO.setName(no + " " +name);
            subPassageDTO.setAverageAccuracy("平均正确率" + averageAccuracy*100 + "%");
            subPassageDTOS.add(subPassageDTO);
        }
        return subPassageDTOS;
    }

    public List<SubPassageDTO> findAll(){
        List<SubPassage> subPassages = subPassageRepository.findAll();
        List<SubPassageDTO> subPassageDTOS = new ArrayList<>();
        for (int i=0;i<subPassages.size();i++){
            SubPassageDTO subPassageDTO = new SubPassageDTO();
            String no = subPassages.get(i).getNo();
            String name = subPassages.get(i).getName();
            String passageId = subPassages.get(i).getPassageId();
            Double averageAccuracy = subPassages.get(i).getAverageAccuracy();
            subPassageDTO.setName(no + " " +name);
            List<PracticeRecord> practiceRecords = practiceRecordRepository.findPracticeRecordsByBelongChapter(no);

            //正确率

            if (practiceRecords.size()==0){
                subPassageDTO.setAverageAccuracy("未开始答题");
            } else{
                int num =0;
                for (int j = 0;j<practiceRecords.size();j++){
                    String[] questions = practiceRecords.get(j).getQuestions().split(",");
                    String[] answers = practiceRecords.get(j).getAnswers().split(",");
                    for (int m=0;m< questions.length;m++){
                        Question question ;
                        question = questionController.findQuestionById(questions[m]);
                        if (question.getCorrectOption().equals(answers[m])){
                            num +=1;
                        }else{
                            num+=0;
                        }
                    }
                }
                double result = (double) num / (practiceRecords.size()*5) *100;
                DecimalFormat decimalFormat = new DecimalFormat("#0");
                subPassageDTO.setAverageAccuracy("平均正确率" + decimalFormat.format(result) + "%");
            }

            subPassageDTO.setPassageId(passageId);
            subPassageDTOS.add(subPassageDTO);
        }
        return subPassageDTOS;
    }

    public SubPassage findByNo(String no){
        return subPassageRepository.findSubPassageByNo(no);
    }


    public Page<SubPassage> findAll(Integer pageNo, Integer pageSize) {
        Pageable pageable = PageRequest.of(pageNo, pageSize);
        return subPassageRepository.findAll(pageable);
    }

    public List<SubPassage> findAllBack() {
        return subPassageRepository.findAll();
    }
}
